
package com.hdoit.ares.logical.brokerage.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.hdoit.ares.logical.acc.model.WithdrawParam;
import com.hdoit.ares.logical.acc.service.IAwardSettingService;
import com.hdoit.ares.logical.acc.service.IWithdrawParamService;
import com.hdoit.ares.logical.brokerage.dao.IBrokerageRecordDao;
import com.hdoit.ares.logical.brokerage.model.BrokerageRecord;
import com.hdoit.ares.logical.brokerage.model.ShareBrokerage;
import com.hdoit.ares.logical.brokerage.service.IBrokerageRecordService;
import com.hdoit.ares.logical.brokerage.service.IShareBrokerageService;
import com.hdoit.ares.logical.cust.model.Customer;
import com.hdoit.ares.logical.cust.service.ICustomerService;
import com.hdoit.ares.logical.viewmodel.brokerage.BrokerageRecordSearch;
import com.hdoit.ares.logical.viewmodel.cust.CustomerSearch;
import com.hdoit.ares.selfcommon.enums.brokerage.ConfirmState;
import com.hdoit.ares.selfcommon.utils.DistributeWealthUtil;
import com.hdoit.ares.selfcommon.utils.ExpressionUtil;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
/**
 * @author      yujinhui
 * @email       
 * @createtime  2017年12月02日 14:27:43
 * @comment
 */
@Component
public class BrokerageRecordServiceImpl extends SuperService<BrokerageRecord> implements IBrokerageRecordService{
    private IBrokerageRecordDao brokerageRecordDao;

	public IBrokerageRecordDao getBrokerageRecordDao() {
		return brokerageRecordDao;
	}
    
	@Resource
	public void setBrokerageRecordDao(IBrokerageRecordDao brokerageRecordDao) {
		this.brokerageRecordDao = brokerageRecordDao;
	}
    
	private IAwardSettingService awardSettingService;
	public IAwardSettingService getAwardSettingService() {
		return awardSettingService;
	}
    
	@Resource
	public void setAwardSettingService(IAwardSettingService awardSettingService) {
		this.awardSettingService = awardSettingService;
	}
	
	private ICustomerService customerService;
	public ICustomerService getCustomerService() {
		return customerService;
	}
    
	@Resource
	public void setCustomerService(ICustomerService customerService) {
		this.customerService = customerService;
	}

	private IShareBrokerageService shareBrokerageService;
	public IShareBrokerageService getShareBrokerageService() {
		return shareBrokerageService;
	}
	
    @Resource
	public void setShareBrokerageService(IShareBrokerageService shareBrokerageService) {
		this.shareBrokerageService = shareBrokerageService;
	}
    
    private IWithdrawParamService withdrawParamService;
    

	public IWithdrawParamService getWithdrawParamService() {
		return withdrawParamService;
	}
	@Resource
	public void setWithdrawParamService(IWithdrawParamService withdrawParamService) {
		this.withdrawParamService = withdrawParamService;
	}

	@Override
	public List<BrokerageRecord> queryList(BrokerageRecordSearch search, PagerModel pagerModel) {
		return brokerageRecordDao.queryList(search,pagerModel);
	}

	@Override
	public List<BrokerageRecord> queryList(BrokerageRecordSearch search) {
		return brokerageRecordDao.queryList(search);
	}

	@Override
	public int queryCount(BrokerageRecordSearch search) {
		return brokerageRecordDao.queryCount(search);
	}
	
	
	@Override
	public boolean applyChange(BrokerageRecord brokerageRecord,String insuranceType) {
		if(super.applyChange(brokerageRecord)){
			if(ModelStatus.add.equals(brokerageRecord.getModelStatus())){
				double promote,firstShare,secondShare,server,hatch;
				
				String str_tax = awardSettingService.queryModelByHC("tax", insuranceType).getExpression();
				double tax = ExpressionUtil.stringToDouble(str_tax,0.00);
				
				double finalPercent = brokerageRecord.getProportion() * tax ;
				
				System.err.println("finalPercent的值:"+finalPercent);
				
				String str_promote = awardSettingService.queryModelByHC("promote", insuranceType).getExpression();
				promote = ExpressionUtil.stringToDouble(str_promote,finalPercent);
				
				String str_firstShare = awardSettingService.queryModelByHC("firstShare", insuranceType).getExpression();
				firstShare = ExpressionUtil.stringToDouble(str_firstShare,finalPercent);
				
				String str_secondShare = awardSettingService.queryModelByHC("secondShare",insuranceType).getExpression();
				secondShare = ExpressionUtil.stringToDouble(str_secondShare,finalPercent);
				
				String str_server = awardSettingService.queryModelByHC("server",insuranceType).getExpression();
				server = ExpressionUtil.stringToDouble(str_server,finalPercent);
				
				String str_hatch = awardSettingService.queryModelByHC("hatch",insuranceType).getExpression();
				hatch = ExpressionUtil.stringToDouble(str_hatch,finalPercent);
		        
		        //得到参与分配奖励的人并且分配奖金
		        List<Customer> customers =  customerService.queryListByBuyer(brokerageRecord.getUserId());
				customers = DistributeWealthUtil.toDistribute(promote, firstShare, secondShare, server, hatch, customers);
				System.err.println("customers的长度" + customers.size());
				
				int groupNum = shareBrokerageService.queryMaxGroupNum();
				
				WithdrawParam  minShareLimit = withdrawParamService.queryModelByHelpCode("minShareLimit");
				//将分配的金额插入佣金分配表中
				List<ShareBrokerage> shareBrokerages = new ArrayList<ShareBrokerage>();
				for(Customer customer:customers){
					ShareBrokerage shraBrokerage = new ShareBrokerage();
					shraBrokerage.setFirstShare(customer.getFirstShare());
					shraBrokerage.setPromote(customer.getPromote());
					shraBrokerage.setSecondShare(customer.getSecondShare());
					shraBrokerage.setServer(customer.getServer());
					shraBrokerage.setHatch(customer.getHatch());
					shraBrokerage.setSort(customer.getSord());
					shraBrokerage.setBrokerageId(brokerageRecord.getId());
					shraBrokerage.setConfirmState(ConfirmState.unConfirm);
					
					shraBrokerage.setCustomerId(customer.getId());
					shraBrokerage.setId(CommonUtils.getKeyId());
					shraBrokerage.setGenerateDate(brokerageRecord.getRecordDate());
					
					shraBrokerage.setGroupNum(groupNum);
					shraBrokerage.setModelStatus(ModelStatus.add);
					
					shareBrokerages.add(shraBrokerage);
				}
				System.err.println("shareBrokerages的长度" + shareBrokerages.size());
				
				if( shareBrokerageService.insertListEx(shareBrokerages) ==  shareBrokerages.size()){
					if(brokerageRecord.getBrokerage() <= minShareLimit.getParam()){
						return shareBrokerageService.insertAccountInfo(brokerageRecord.getId(),null);
					}
					return true;
				}
				return false;
			}
			return true;
		}
		return false;
	}

	@Override
	public HashMap<String,String> queryBrokerageRecordForStatistic(CustomerSearch search) {

		HashMap<String,String> map = new HashMap<String,String>();
		
		Calendar now = Calendar.getInstance();
		//获取当前时间
		now.setTime(DateUtils.getCurrent());
		//获取当前月份的天数
		int days = now.getActualMaximum(Calendar.DATE);
		
		for(int i = 1; i <= days; i++){
			map.put(String.valueOf(i),String.valueOf(0));
		}
		
		List<BrokerageRecord> brokerageRecords =brokerageRecordDao.queryBrokerageRecordForStatistic(search);
	    
		
			for(BrokerageRecord brokerageRecord:brokerageRecords){
				now.setTime(brokerageRecord.getRecordDate());
				String key = "" + now.get(Calendar.DAY_OF_MONTH);
				int value = Integer.valueOf(map.get(key)) + 1;
				map.replace(key, String.valueOf(value));
			}
		
		return map;
	}

	@Override
	public HashMap<String, String> queryBrokerageRecordForMoney(CustomerSearch search) {
		HashMap<String,String> map = new HashMap<String,String>();
		
		Calendar now = Calendar.getInstance();
		//获取当前时间
		now.setTime(DateUtils.getCurrent());
		//获取当前月份的天数
		int days = now.getActualMaximum(Calendar.DATE);
		
		for(int i = 1; i <= days; i++){
			map.put(String.valueOf(i),String.valueOf(0));
		}
		
		List<BrokerageRecord> brokerageRecords =brokerageRecordDao.queryBrokerageRecordForStatistic(search);
	    
		
			for(BrokerageRecord brokerageRecord:brokerageRecords){
				now.setTime(brokerageRecord.getRecordDate());
				String key = "" + now.get(Calendar.DAY_OF_MONTH);
				double value = Double.valueOf(map.get(key)) + brokerageRecord.getBrokerage();
				map.replace(key, String.valueOf(value));
			}
		
		return map;
	}
	
}

